/*******************************************************************************
 * Package: com.hngtrust.mcms.image.service
 * Type:    ExcelDownloadSeviceImpl
 * Author:  Mahuili
 * Date:    2020/6/2 20:32
 *
 * Copyright (c) 2020 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.risk.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.hngtrust.iir.risk.service.ExcelDownloadService;
import com.tiger.excel.ExcelUtils;
import com.tiger.excel.ExportConfig;
import com.tiger.web.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 导出excel服务类实现类
 *
 * @author Mahuili
 * @date 2020/6/2 20:32
 */
@Service
public class ExcelDownloadServiceImpl implements ExcelDownloadService {
    
    /**
     * 文件服务
     */
    @Autowired
    @Qualifier("localFileService")
    private FileService fileService;
   
    
    /**
     * 导出组装实现类
     *
     * @param dataList
     * @param response
     * @param fileName
     * @param <T>
     * @throws Exception
     */
    @Override
    public <T> void excelDownload(List<T> dataList, HttpServletResponse response, String fileName) throws Exception {
        
       
        //时间戳处理，避免重名
        StringBuffer fileNameStr = new StringBuffer();
        //年月日时分秒
        String dateStr = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
        fileName = fileNameStr.append(fileName).append("-").append(dateStr).append("-").toString();
        File fileTemp = File.createTempFile(fileName, ".xls");
        
        //生成excel文件
        if(dataList.size() > 0){
            ExcelUtils.export2Excel(dataList, fileTemp);
        }
        //下载excel文件
        fileService.download(fileTemp.getPath(), response);
        fileTemp.deleteOnExit();
        
    }
    
    /**
     * 导出组装实现类
     *
     * @param dataList
     * @param response
     * @param fileName
     * @param <T>
     * @throws Exception
     */
    @Override
    public <T> void excelDownload(List<T> dataList, HttpServletResponse response, String fileName,
            Boolean isCreateHeadRows) throws Exception {
        //时间戳处理，避免重名
        StringBuffer fileNameStr = new StringBuffer();
        //年月日时分秒
        String dateStr = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
        fileName = fileNameStr.append(fileName).append("-").append(dateStr).append("-").toString();
        File fileTemp = File.createTempFile(fileName, ".xls");
        
        //生成excel文件
        ExcelUtils.export2Excel(dataList, fileTemp, ExportConfig.builder().isCreateHeadRows(isCreateHeadRows).build());
        //下载excel文件
        fileService.download(fileTemp.getPath(), response);
        fileTemp.deleteOnExit();
        
    }
}
